//
//  SettingController.m
//  SleepingElf
//
//  Created by karong on 2023/10/26.
//

#import "SettingController.h"
#import "SettingCell.h"

@interface SettingController ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@end

@implementation SettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SettingCell * settingCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SettingCell" forIndexPath:indexPath];
    [settingCell setDataWithObject:self.dataArray[indexPath.row]];
    return settingCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = SEScreenWidth;
    size.height = 44;
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        [[SDImageCache sharedImageCache] clearMemory];
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
        [SEToast simple:WSLocalized(@"操作成功")];
        self.dataArray = nil;
        [self.listView reloadData];
    }
    else if (indexPath.row == 1) {
        UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"AboutController"];
        [self.navigationController pushViewController:target animated:YES];
    }
}

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@{@"title":WSLocalized(@"清除缓存"),
                         @"content":SEEmptyStr([self cacheSize])},
                       @{@"title":WSLocalized(@"关于我们"),
                         @"content":@""}];
    }
    return _dataArray;
}

- (NSString *)cacheSize {
    NSUInteger size = [[SDImageCache sharedImageCache] totalDiskSize];
    NSString *cacheStr = [Tools fileSizeWithInterge:size];
    return cacheStr;
}

@end
